package com.store.app.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.Result;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.store.app.ExitApplication;
import com.store.app.LocationApplication;
import com.store.app.MainActivity;
import com.store.app.R;
import com.store.app.bean.DJMBean;
import com.store.app.bean.PayCodeBean;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.utils.MoneyUtil;
import com.store.app.utils.ToastUtils;
import com.store.app.utils.Utils;
import com.store.app.zxing.Capture_ZxingActivity;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 首页扫一扫页面
 */
public class HomeCaptureActivity extends Capture_ZxingActivity implements IOnHttpListener, OnClickListener {
    private NetRequest netRequest;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                setReCapture();
            }
        }
    };

    private ImageView ivCashCoupon;
    private TextView tvCashCoupon;
    private LinearLayout llCashCoupon;      // 现金券
    private ImageView img_scan_pay;
    private TextView tv_scan_pay;
    private LinearLayout ll_scan_pay;       //付款码
    private ImageView img_family_card;
    private TextView tv_family_card;
    private LinearLayout ll_family_card;    //亲情卡
    private ImageView iv_member;
    private TextView tv_member;
    private LinearLayout ll_member;         //解冻码
    private ImageView img_validate_coupon;
    private TextView tv_validate_coupon;
    private LinearLayout ll_validate_coupon;//优惠券
    private ImageView iv_duijiangma;
    private TextView tv_duijiangma;
    private LinearLayout ll_duijiangma;     //兑奖码
    private TextView pay_amount;            //收款金额
    private LinearLayout ll_ok_get_money;   //确认收款
    private LinearLayout ll_get_money;      //收款金额底部控件

    private final int STATE_FAMILY_CARD = 1;        //亲情卡
    private final int STATE_SCAN_PAY = 2;           //付款码
    private final int STATE_MEMEBER_PAY = 3;        //解冻码
    private final int STATE_VALIDATE_COUPON = 4;    //优惠券
    private final int STATE_DUIJIANGMA = 5;         //兑奖码
    private final int STATE_CASH_COUPON = 6;        // 现金券
    private int currentState = STATE_CASH_COUPON;   // 当前处于哪个状态扫码状态

    private PayCodeBean payCodeBean;//扫码付款码的交易bean
    private String sktCode;//扫码得到优惠券的码
    private Dialog dialogCoupon;//优惠券信息查看弹窗
    private Dialog dialogJDM;//解冻码弹窗
    private Dialog dialogDJM;//兑奖码弹窗
    private TextView tv_dialog_success_content;
    private TextView tv_success_ok;
    private RelativeLayout rl_dialog_success;//验证成功弹窗
    private String consumerId;//要解冻的会员的id
    private DJMBean djmBean;//兑奖码信息
    private TextView tv_get_reward;            //兑奖获得奖品
    private TextView tv_duijiangma_ok;          //兑奖成功我知道了
    private RelativeLayout rl_duijiang_success;//兑奖成功弹窗
    private String payContent;//付款码内容

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        netRequest = new NetRequest(this);
        ExitApplication.getInstance().addActivity(this);
        setContentView(R.layout.activity_home_capture);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        // setInintView();
        initView();
    }

    @Override
    public void onAfinalFail() {
        dismissProgressDialog();
        showToast("请检查本地网络！");
    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        dismissProgressDialog();
        //通过亲情卡号查询会员信息
        if (requestId == 1) {
            try {
                JSONObject jsonObject = new JSONObject(result);
                String member_id = jsonObject.getString("member_id");
                String cash_balance = jsonObject.getString("cash_balance");
                String nick_name = jsonObject.optString("nick_name");
                String gold_balance = jsonObject.getString("gold");
                if (TextUtils.isEmpty(nick_name)) {
                    nick_name = "暂无昵称";
                }
                showMemberInfoDialog(cash_balance, gold_balance, nick_name);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //扫到付款码之后调交易验证接口成功
        else if (requestId == 2) {
            //底部收款金额整体控件
            ll_get_money.setVisibility(View.VISIBLE);

        }
        //付款码支付成功
        else if (requestId == 3) {
            Toast.makeText(getApplicationContext(), "收款成功", Toast.LENGTH_SHORT).show();
            ll_get_money.setVisibility(View.GONE);
        }
        //查询优惠券信息
        else if (requestId == 4) {
            showCouponInfoDialog();
        }
        //验证优惠券成功
        else if (requestId == 5) {
            rl_dialog_success.setVisibility(View.VISIBLE);
        }
        //查询解冻码信息
        else if (requestId == 6) {
            try {
                JSONObject jsonObject = new JSONObject(result);
                String cash_balance = jsonObject.getString("cash_balance");
                String gold = jsonObject.getString("gold");
                String total_num = jsonObject.getString("total_num");
                String violation_num = jsonObject.getString("violation_num");
                String mobile = jsonObject.getString("mobile");
                String cash_froze = jsonObject.getString("cash_froze");
                showJDMInfoDialog(mobile, cash_balance, cash_froze, gold, total_num, violation_num);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //解冻成功
        else if (requestId == 7) {
            showToast("解冻成功");
        }
        //兑奖码信息查询成功
        else if (requestId == 8) {
            try {
                JSONObject jsonObject = new JSONObject(result);
                String activity_type = jsonObject.getString("activity_type");
                String award_name = jsonObject.getString("award_name");
                String award_pic_path1_url = jsonObject.getString("award_pic_path1_url");
                String created_date = jsonObject.getString("created_date");
                String title = jsonObject.getString("title");
                showDJMInfoDialog(activity_type, award_name, award_pic_path1_url, created_date, title);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //兑奖成功
        else if (requestId == 9) {
            try {
                JSONObject jsonObject = new JSONObject(result);
                String award_name = jsonObject.getString("award_name");//商品名称
                String content = "奖品:" + award_name;
                if (!TextUtils.isEmpty(award_name)) {
                    tv_get_reward.setText(content);
                }
                rl_duijiang_success.setVisibility(View.VISIBLE);
            } catch (Exception e) {
                e.printStackTrace();
            }

        } else if (requestId == 11) {
            rl_dialog_success.setVisibility(View.VISIBLE);
        }
    }

    public void showDJMInfoDialog(String activity_type, String award_name, String award_pic_path1_url, String created_date, String title) {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
        dialogBuilder.create();
        LayoutInflater inflater = LayoutInflater.from(this);
        View temp = inflater.inflate(R.layout.dialog_duijiangma, null);
        TextView tv_ok = (TextView) temp.findViewById(R.id.tv_ok);
        TextView tv_cancel = (TextView) temp.findViewById(R.id.tv_cancel);
        TextView tv_activity_name = (TextView) temp.findViewById(R.id.tv_activity_name);
        TextView tv_activity_type = (TextView) temp.findViewById(R.id.tv_activity_type);
        TextView tv_time = (TextView) temp.findViewById(R.id.tv_time);
        TextView tv_title = (TextView) temp.findViewById(R.id.tv_title);
        ImageView iv = (ImageView) temp.findViewById(R.id.iv);
        String tempName = activity_type;
        if (tempName.equals("YYY")) {
            tempName = "摇一摇";
        } else if (tempName.equals("GGL")) {
            tempName = "刮刮乐";
        } else if (tempName.equals("DSK")) {
            tempName = "定时开";
        }
        tv_activity_name.setText(award_name);
        tv_activity_type.setText(tempName);
        tv_time.setText(created_date);
        tv_title.setText(title);
        ImageLoader.getInstance().displayImage(award_pic_path1_url, iv, options);
        tv_ok.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (djmBean != null) {
                    dialogDJM.dismiss();
                    netRequest.requestValidateCode(9, MainActivity.member_id, djmBean.getActivity_id(), djmBean.getConsumer_id(), djmBean.getDraw_code(), djmBean.getActivity_type(), djmBean.getProcess_id());
                }
            }
        });
        tv_cancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                dialogDJM.dismiss();
            }
        });
        dialogBuilder.setView(temp);
        dialogDJM = dialogBuilder.show();
    }

    /**
     * @param mobile        会员账号
     * @param cash_balance  零钱余额
     * @param cash_froze    冻结金额
     * @param gold          金币余额
     * @param total_num     领样记录
     * @param violation_num 违规记录
     */
    public void showJDMInfoDialog(String mobile, String cash_balance, final String cash_froze, String gold, String total_num, String violation_num) {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
        dialogBuilder.create();
        LayoutInflater inflater = LayoutInflater.from(this);
        View temp = inflater.inflate(R.layout.dialog_jiedongma, null);
        TextView tv_thaw = (TextView) temp.findViewById(R.id.tv_thaw);
        TextView tv_cancel = (TextView) temp.findViewById(R.id.tv_cancel);
        TextView tv_phone = (TextView) temp.findViewById(R.id.tv_phone);
        TextView tv_banlce = (TextView) temp.findViewById(R.id.tv_banlce);
        TextView tv_ban = (TextView) temp.findViewById(R.id.tv_ban);
        TextView tv_gold = (TextView) temp.findViewById(R.id.tv_gold);
        TextView tv_free = (TextView) temp.findViewById(R.id.tv_free);
        TextView tv_Illegal = (TextView) temp.findViewById(R.id.tv_Illegal);
        tv_phone.setText(mobile);
        tv_banlce.setText(cash_balance);
        tv_ban.setText(cash_froze);
        tv_gold.setText(gold);
        tv_free.setText(total_num);
        tv_Illegal.setText(violation_num);
        tv_thaw.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialogJDM.dismiss();
                if (!MoneyUtil.moneyBigThan(cash_froze, "0")) {
                    showToast("无需解冻");
                } else {
                    if (!TextUtils.isEmpty(consumerId)) {
                        netRequest.requestStoresMemberRelCreate(10,consumerId);
                        netRequest.requestStoresUnfreezeBalanceForConsumer(7, consumerId);
                    }
                }
            }
        });
        tv_cancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                dialogJDM.dismiss();
            }
        });
        dialogBuilder.setView(temp);
        dialogJDM = dialogBuilder.show();
    }

    /**
     * 查看优惠券信息弹窗
     */
    public void showCouponInfoDialog() {
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
        dialogBuilder.create();
        LayoutInflater inflater = LayoutInflater.from(this);
        View temp = inflater.inflate(R.layout.dialog_youhuiquan, null);
        TextView tv_ok = (TextView) temp.findViewById(R.id.tv_ok);
        TextView tv_cancel = (TextView) temp.findViewById(R.id.tv_cancel);
        tv_ok.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(MainActivity.member_id) && !TextUtils.isEmpty(sktCode)) {
                    dialogCoupon.dismiss();
                    //调验证优惠券接口
                    netRequest.requestCouponValidate(5, sktCode);
                }
            }
        });
        tv_cancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                dialogCoupon.dismiss();
            }
        });
        dialogBuilder.setView(temp);
        dialogCoupon = dialogBuilder.show();
    }

    public void showMemberInfoDialog(String cash_balance, String gold_balance, String nick_name) {
        AlertDialog.Builder builder1 = new AlertDialog.Builder(HomeCaptureActivity.this);
        builder1.setTitle(R.string.member_info);
        final LayoutInflater inflater1 = LayoutInflater.from(HomeCaptureActivity.this);
        View v1 = inflater1.inflate(R.layout.member_information, null);
        TextView name = (TextView) v1.findViewById(R.id.name);
        name.setText(nick_name);
        TextView number = (TextView) v1.findViewById(R.id.number);
        number.setText(cash_balance);
        TextView consumption = (TextView) v1.findViewById(R.id.consumption);
        consumption.setText(gold_balance);
        builder1.setView(v1);
        builder1.setNegativeButton(R.string.soft_close, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        Dialog mDownloadDialog = builder1.create();
        mDownloadDialog.show();
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {
        dismissProgressDialog();
        ToastUtils.showTextToast(HomeCaptureActivity.this, msg);

        if (requestId == 3) {
            ll_get_money.setVisibility(View.GONE);
        }
        if (requestId == 11) {
            showToast(msg);
        }
    }

    private void initView() {
        TextView tv_cap = (TextView) findViewById(R.id.cap_tv);
        findViewById(R.id.retreat).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("扫描二维码");
        int width = LocationApplication.app.getDM().widthPixels;
        int high = LocationApplication.app.getDM().heightPixels;
        width = width / 8 * 3;
        high = high / 2 - width;
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.topMargin = high - MainActivity.dip2px(60);
        layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
        tv_cap.setLayoutParams(layoutParams);
        tv_cap.setText("将二维码放入框内,即可自动扫描");

        ivCashCoupon = (ImageView) findViewById(R.id.img_cash_coupon);
        tvCashCoupon = (TextView) findViewById(R.id.tv_cash_coupon);
        llCashCoupon = (LinearLayout) findViewById(R.id.ll_cash_coupon);

        img_scan_pay = (ImageView) findViewById(R.id.img_scan_pay);
        tv_scan_pay = (TextView) findViewById(R.id.tv_scan_pay);
        ll_scan_pay = (LinearLayout) findViewById(R.id.ll_scan_pay);

        img_family_card = (ImageView) findViewById(R.id.img_family_card);
        tv_family_card = (TextView) findViewById(R.id.tv_family_card);
        ll_family_card = (LinearLayout) findViewById(R.id.ll_family_card);

        iv_member = (ImageView) findViewById(R.id.iv_member);
        tv_member = (TextView) findViewById(R.id.tv_member);
        ll_member = (LinearLayout) findViewById(R.id.ll_member);

        img_validate_coupon = (ImageView) findViewById(R.id.img_validate_coupon);
        tv_validate_coupon = (TextView) findViewById(R.id.tv_validate_coupon);
        ll_validate_coupon = (LinearLayout) findViewById(R.id.ll_validate_coupon);

        iv_duijiangma = (ImageView) findViewById(R.id.iv_duijiangma);
        tv_duijiangma = (TextView) findViewById(R.id.tv_duijiangma);
        ll_duijiangma = (LinearLayout) findViewById(R.id.ll_duijiangma);

        pay_amount = (TextView) findViewById(R.id.pay_amount);
        ll_ok_get_money = (LinearLayout) findViewById(R.id.ll_ok_get_money);
        ll_ok_get_money.setOnClickListener(this);
        ll_get_money = (LinearLayout) findViewById(R.id.ll_get_money);
        ll_get_money.setOnClickListener(this);

        //底部五个选项监听
        llCashCoupon.setOnClickListener(this);
        ll_scan_pay.setOnClickListener(this);
        ll_family_card.setOnClickListener(this);
        ll_member.setOnClickListener(this);
        ll_validate_coupon.setOnClickListener(this);
        ll_duijiangma.setOnClickListener(this);
        tv_dialog_success_content = (TextView) findViewById(R.id.tv_dialog_success_content);
        tv_success_ok = (TextView) findViewById(R.id.tv_success_ok);
        tv_success_ok.setOnClickListener(this);
        rl_dialog_success = (RelativeLayout) findViewById(R.id.rl_dialog_success);
        tv_get_reward = (TextView) findViewById(R.id.tv_get_reward);
        tv_duijiangma_ok = (TextView) findViewById(R.id.tv_duijiangma_ok);
        tv_duijiangma_ok.setOnClickListener(this);
        rl_duijiang_success = (RelativeLayout) findViewById(R.id.rl_duijiang_success);
    }


    /**
     * 处理扫描结果
     *
     * @param result
     * @param barcode
     */
    public void handleDecode(Result result, Bitmap barcode) {
        inactivityTimer.onActivity();
        playBeepSoundAndVibrate();
        String resultString = result.getText();
        handler.sendEmptyMessageDelayed(1, 3000);
        if (!TextUtils.isEmpty(resultString)) {

            //当前只扫亲情卡
            if (currentState == STATE_FAMILY_CARD) {
                if (resultString.length() > 3) {
                    String identifying = resultString.substring(0, 3);
                    if (identifying.equals("MTH")) {
                        //获取会员信息
                        showProgressDialog("","正在查询亲情卡信息...");
                        netRequest.requestCheckFamilyCard(1, resultString);
                    } else {
                        Toast.makeText(HomeCaptureActivity.this, "此二维码非亲情卡！", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(HomeCaptureActivity.this, "此二维码非亲情卡！", Toast.LENGTH_SHORT).show();
                }
            }
            //当前只扫付款码
            else if (currentState == STATE_SCAN_PAY) {
                try {
                    //条形码
                    JSONObject object = new JSONObject(resultString);
                    String type = object.getString("type");
                    if (type.equals("P")) {
                        payContent = resultString;
                        String flow_no = object.getString("flow_no");
                        String security_code = object.getString("security_code");
                        pay_amount.setText(object.getString("amount") + "");
                        Drawable drawable = getResources().getDrawable(R.drawable.border_scan);
                        ll_ok_get_money.setBackground(drawable);
                        ll_ok_get_money.setClickable(true);
                        showProgressDialog("","正在验证交易凭证...");
                        netRequest.requestTradeVerify(2, flow_no, security_code);
                    }
                } catch (JSONException e) {
                    Toast.makeText(HomeCaptureActivity.this, "此二维码非付款码！", Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
            }
            //当前只扫优惠券
            else if (currentState == STATE_VALIDATE_COUPON) {
                if (resultString.length() > 3) {
                    String identifying = resultString.substring(0, 3);
                    if (identifying.equals("YHQ")) {
                        //窃取YHQ的后面的码
                        sktCode = resultString.substring(3, resultString.length());
                        showProgressDialog("","正在查询优惠券信息...");
                        netRequest.requestValidateFindForStores(4, sktCode);
                    } else {
                        Toast.makeText(HomeCaptureActivity.this, "此二维码非优惠券！", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(HomeCaptureActivity.this, "此二维码非优惠券！", Toast.LENGTH_SHORT).show();
                }
            }
            //当前只扫解冻码
            else if (currentState == STATE_MEMEBER_PAY) {
                consumerId = resultString;
                showProgressDialog("","正在查询会员信息...");
                netRequest.requestUnfreeze_info(6, resultString);
            }
            //当前只扫兑奖码
            else if (currentState == STATE_DUIJIANGMA) {
                try {
                    JSONObject jsonObject = new JSONObject(resultString);
                    String draw_code = jsonObject.getString("draw_code");
                    String activity_id = jsonObject.getString("activity_id");
                    String consumer_id = jsonObject.getString("consumer_id");
                    String activity_type = jsonObject.getString("activity_type");
                    String process_id = jsonObject.getString("process_id");
                    djmBean = new DJMBean();
                    djmBean.setActivity_type(activity_type);
                    djmBean.setDraw_code(draw_code);
                    djmBean.setConsumer_id(consumer_id);
                    djmBean.setProcess_id(process_id);
                    djmBean.setActivity_id(activity_id);

                    if (!TextUtils.isEmpty(activity_type)
                            && !TextUtils.isEmpty(MainActivity.member_id)
                            && !TextUtils.isEmpty(activity_id)
                            && !TextUtils.isEmpty(process_id)
                            && !TextUtils.isEmpty(draw_code)) {
                        showProgressDialog("","正在查询兑奖码信息...");
                        netRequest.requestValidateFind(8, activity_type, MainActivity.member_id, activity_id, process_id, draw_code);
                    } else {
                        Toast.makeText(HomeCaptureActivity.this, "二维码信息不完整", Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(HomeCaptureActivity.this, "此二维码非兑奖码！", Toast.LENGTH_SHORT).show();
                }

            } else if (currentState == STATE_CASH_COUPON) {
                // 将非空的 resultString 传给后台。
                netRequest.requestScanningCashCoupon(11, resultString);
            }
        } else {
            ToastUtils.showTextToast(HomeCaptureActivity.this, "无法识别此二维码");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_cash_coupon:
                currentState = STATE_CASH_COUPON;

                tvCashCoupon.setTextColor(getResources().getColor(R.color.orange1));
                tv_scan_pay.setTextColor(getResources().getColor(R.color.text_color_capture_gray));
                tv_family_card.setTextColor(getResources().getColor(R.color.text_color_capture_gray));
                tv_member.setTextColor(getResources().getColor(R.color.text_color_capture_gray));
                tv_validate_coupon.setTextColor(getResources().getColor(R.color.text_color_capture_gray));
                tv_duijiangma.setTextColor(getResources().getColor(R.color.text_color_capture_gray));

                ivCashCoupon.setBackgroundResource(R.drawable.scan_cash_coupon_selected);
                img_scan_pay.setBackgroundResource(R.drawable.capture_scan_pay_check);
                img_family_card.setBackgroundResource(R.drawable.capture_family_card1);
                iv_member.setBackgroundResource(R.drawable.saohuima);
                img_validate_coupon.setBackgroundResource(R.drawable.capture_coupon_check);
                iv_duijiangma.setBackgroundResource(R.drawable.duijiangma_no_choose);
                break;
            //亲情卡扫描
            case R.id.ll_family_card:
                currentState = STATE_FAMILY_CARD;

                tvCashCoupon.setTextColor(getResources().getColor(R.color.text_color_capture_gray));
                tv_scan_pay.setTextColor(getResources().getColor(R.color.text_color_capture_gray));
                tv_family_card.setTextColor(getResources().getColor(R.color.orange1));
                tv_member.setTextColor(getResources().getColor(R.color.text_color_capture_gray));
                tv_validate_coupon.setTextColor(getResources().getColor(R.color.text_color_capture_gray));
                tv_duijiangma.setTextColor(getResources().getColor(R.color.text_color_capture_gray));

                ivCashCoupon.setBackgroundResource(R.drawable.scan_cash_coupon);
                img_scan_pay.setBackgroundResource(R.drawable.capture_scan_pay_check);
                img_family_card.setBackgroundResource(R.drawable.capture_family_card_check);
                iv_member.setBackgroundResource(R.drawable.saohuima);
                img_validate_coupon.setBackgroundResource(R.drawable.capture_coupon_check);
                iv_duijiangma.setBackgroundResource(R.drawable.duijiangma_no_choose);
                break;
            //付款码扫描
            case R.id.ll_scan_pay:
                currentState = STATE_SCAN_PAY;

                tvCashCoupon.setTextColor(getResources().getColor(R.color.text_color_capture_gray));
                tv_scan_pay.setTextColor(getResources().getColor(R.color.orange1));
                tv_family_card.setTextColor(getResources().getColor(R.color.text_color_capture_gray));
                tv_member.setTextColor(getResources().getColor(R.color.text_color_capture_gray));
                tv_validate_coupon.setTextColor(getResources().getColor(R.color.text_color_capture_gray));
                tv_duijiangma.setTextColor(getResources().getColor(R.color.text_color_capture_gray));

                ivCashCoupon.setBackgroundResource(R.drawable.scan_cash_coupon);
                img_scan_pay.setBackgroundResource(R.drawable.capture_scan_pay);
                img_family_card.setBackgroundResource(R.drawable.capture_family_card1);
                iv_member.setBackgroundResource(R.drawable.saohuima);
                img_validate_coupon.setBackgroundResource(R.drawable.capture_coupon_check);
                iv_duijiangma.setBackgroundResource(R.drawable.duijiangma_no_choose);
                break;
            //解冻码
            case R.id.ll_member:
                currentState = STATE_MEMEBER_PAY;

                tvCashCoupon.setTextColor(getResources().getColor(R.color.text_color_capture_gray));
                tv_scan_pay.setTextColor(getResources().getColor(R.color.text_color_capture_gray));
                tv_family_card.setTextColor(getResources().getColor(R.color.text_color_capture_gray));
                tv_member.setTextColor(getResources().getColor(R.color.orange1));
                tv_validate_coupon.setTextColor(getResources().getColor(R.color.text_color_capture_gray));
                tv_duijiangma.setTextColor(getResources().getColor(R.color.text_color_capture_gray));

                ivCashCoupon.setBackgroundResource(R.drawable.scan_cash_coupon);
                img_scan_pay.setBackgroundResource(R.drawable.capture_scan_pay_check);
                img_family_card.setBackgroundResource(R.drawable.capture_family_card1);
                iv_member.setBackgroundResource(R.drawable.red_saohuima);
                img_validate_coupon.setBackgroundResource(R.drawable.capture_coupon_check);
                iv_duijiangma.setBackgroundResource(R.drawable.duijiangma_no_choose);
                break;
            //优惠券
            case R.id.ll_validate_coupon:
                currentState = STATE_VALIDATE_COUPON;

                tvCashCoupon.setTextColor(getResources().getColor(R.color.text_color_capture_gray));
                tv_scan_pay.setTextColor(getResources().getColor(R.color.text_color_capture_gray));
                tv_family_card.setTextColor(getResources().getColor(R.color.text_color_capture_gray));
                tv_member.setTextColor(getResources().getColor(R.color.text_color_capture_gray));
                tv_validate_coupon.setTextColor(getResources().getColor(R.color.orange1));
                tv_duijiangma.setTextColor(getResources().getColor(R.color.text_color_capture_gray));

                ivCashCoupon.setBackgroundResource(R.drawable.scan_cash_coupon);
                img_scan_pay.setBackgroundResource(R.drawable.capture_scan_pay_check);
                img_family_card.setBackgroundResource(R.drawable.capture_family_card1);
                iv_member.setBackgroundResource(R.drawable.saohuima);
                img_validate_coupon.setBackgroundResource(R.drawable.capture_coupon);
                iv_duijiangma.setBackgroundResource(R.drawable.duijiangma_no_choose);
                break;
            //兑奖码
            case R.id.ll_duijiangma:
                currentState = STATE_DUIJIANGMA;

                tvCashCoupon.setTextColor(getResources().getColor(R.color.text_color_capture_gray));
                tv_scan_pay.setTextColor(getResources().getColor(R.color.text_color_capture_gray));
                tv_family_card.setTextColor(getResources().getColor(R.color.text_color_capture_gray));
                tv_member.setTextColor(getResources().getColor(R.color.text_color_capture_gray));
                tv_validate_coupon.setTextColor(getResources().getColor(R.color.text_color_capture_gray));
                tv_duijiangma.setTextColor(getResources().getColor(R.color.orange1));

                ivCashCoupon.setBackgroundResource(R.drawable.scan_cash_coupon);
                iv_member.setBackgroundResource(R.drawable.saohuima);
                img_family_card.setBackgroundResource(R.drawable.capture_family_card1);
                img_scan_pay.setBackgroundResource(R.drawable.capture_scan_pay_check);
                img_validate_coupon.setBackgroundResource(R.drawable.capture_coupon_check);
                iv_duijiangma.setBackgroundResource(R.drawable.duijiangma_choose);
                break;
            //确认收款
            case R.id.ll_ok_get_money:
                showProgressDialog("","正在收款...");
                if (Utils.isFastClick()) {
                    return;
                }
                try {
                    JSONObject jsonObject = new JSONObject(payContent);
//                    Drawable drawable = getResources().getDrawable(R.drawable.border_scan);
//                    //修改确认收款金额按钮背景颜色
//                    ll_ok_get_money.setBackground(drawable);
//                    ll_ok_get_money.setClickable(true);
                    //调支付接口
                    String amount = jsonObject.getString("amount");
                    String member_id = jsonObject.getString("member_id");
                    String flow_no = jsonObject.getString("flow_no");
                    String security_code = jsonObject.getString("security_code");
                    pay_amount.setText(amount);
                    payCodeBean = new PayCodeBean();
                    payCodeBean.setAmount(amount);
                    payCodeBean.setMember_id(member_id);
                    payCodeBean.setFlow_no(flow_no);
                    payCodeBean.setSecurity_code(security_code);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (payCodeBean != null) {
                    ll_ok_get_money.setClickable(false);
                    ll_ok_get_money.setBackgroundResource(R.drawable.border_scan_gray);
                    netRequest.requestStoresMemberRelCreate(10,payCodeBean.getMember_id());
                    netRequest.requestScanPay(3, payCodeBean.getMember_id(), payCodeBean.getAmount(), payCodeBean.getFlow_no(), payCodeBean.getSecurity_code());
                }
                break;
            //验证成功中的我知道了
            case R.id.tv_success_ok:
                rl_dialog_success.setVisibility(View.GONE);
                break;
            //兑奖成功我知道了
            case R.id.tv_duijiangma_ok:
                rl_duijiang_success.setVisibility(View.GONE);
                break;

        }
    }

    DisplayImageOptions options = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .showImageOnLoading(R.drawable.default_picture_s)
            .showImageOnFail(R.drawable.default_picture_s)
            .showStubImage(R.drawable.default_picture_s)
            .imageScaleType(ImageScaleType.EXACTLY)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();
}
